home *** CD-ROM | disk | FTP | other *** search
/ Workbench Add-On / Workbench Add-On - Volume 1.iso / BBS-Archive / DiskUtil / Crunch / XFH.lha / XFH / SRC / PACKET.C < prev    next >
C/C++ Source or Header  |  1994-03-10  |  4KB  |  117 lines

  1. /* Packet.c - general, lo-level packet handling routines.
  2.    Copyright (C) 1991, 1992, 1993 Kristian Nielsen.
  3.  
  4.    This file is part of XFH, the compressing file system handler.
  5.  
  6.    This program is free software; you can redistribute it and/or modify
  7.    it under the terms of the GNU General Public License as published by
  8.    the Free Software Foundation; either version 2 of the License, or
  9.    (at your option) any later version.
  10.  
  11.    This program is distributed in the hope that it will be useful,
  12.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.    GNU General Public License for more details.
  15.  
  16.    You should have received a copy of the GNU General Public License
  17.    along with this program; if not, write to the Free Software
  18.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.            */
  19.  
  20. #include "CFS.h"
  21.  
  22. #include <stdarg.h>
  23.  
  24. /*
  25.  * Send an already allocate packet, with given arguments, to a handler.
  26.  * retport is the port that the packet should return to. This function will
  27.  * NOT wait for the packet to return. numarg specifies how many arguments
  28.  * follow after it (arguments will be treated as LONG).
  29.  *
  30.  * Use this function for doing async IO with DOS handlers.
  31.  */
  32.  
  33. void putpkt(struct StandardPacket *pkt,struct MsgPort *procid,
  34.            struct MsgPort *retport,LONG type,int numarg,...){
  35.    va_list vl;
  36.    LONG *p;
  37.    
  38.    /* Allow for variable number of parameters in a packet. */
  39.    va_start(vl,numarg);
  40.    
  41.    /* Set up fields for packet handling. */
  42.    pkt->sp_Msg.mn_Node.ln_Succ=pkt->sp_Msg.mn_Node.ln_Pred=NULL;
  43.    pkt->sp_Msg.mn_Node.ln_Name=(char *)&pkt->sp_Pkt;
  44.    pkt->sp_Msg.mn_Node.ln_Type=NT_MESSAGE;
  45.    pkt->sp_Msg.mn_Node.ln_Pri=0;
  46.    pkt->sp_Msg.mn_ReplyPort=NULL;
  47.    pkt->sp_Msg.mn_Length=sizeof(*pkt);
  48.    pkt->sp_Pkt.dp_Port=retport;
  49.    pkt->sp_Pkt.dp_Link=&pkt->sp_Msg;
  50.  
  51.    /* Set up packet args. First the packet request type. */
  52.    pkt->sp_Pkt.dp_Type=type;
  53.    
  54.    /* Handle each of the arguments in turn. */
  55.    p=&pkt->sp_Pkt.dp_Arg1;
  56.    while(numarg--){
  57.       *p++=va_arg(vl,LONG);
  58.    }
  59.    
  60.    /* And finally, set the packet off on its way to the handler. */
  61.    PutMsg(procid,&pkt->sp_Msg);
  62.    
  63.    va_end(vl);
  64. }
  65.  
  66.  
  67. /*
  68.  * Send an already allocated packet to an AmigaDOS handler using
  69.  * supplied arguments, wait for the reply and return dp_Res1 & dp_Res2.
  70.  *
  71.  * Use this for doing syncronous IO just like dos.library, but using
  72.  * a private message port.
  73.  */
  74.  
  75. LONG dopkt(struct StandardPacket *pkt,struct MsgPort *procid,
  76.            struct MsgPort *retport,LONG *res2,LONG type,int numarg,...){
  77.    va_list vl;
  78.    LONG *p;
  79.    struct Message *msg;
  80.    
  81.    /* Set up fields for packet handling. */
  82.    pkt->sp_Msg.mn_Node.ln_Succ=pkt->sp_Msg.mn_Node.ln_Pred=NULL;
  83.    pkt->sp_Msg.mn_Node.ln_Name=(char *)&pkt->sp_Pkt;
  84.    pkt->sp_Msg.mn_Node.ln_Type=NT_MESSAGE;
  85.    pkt->sp_Msg.mn_Node.ln_Pri=0;
  86.    pkt->sp_Msg.mn_ReplyPort=NULL;
  87.    pkt->sp_Msg.mn_Length=sizeof(*pkt);
  88.    pkt->sp_Pkt.dp_Port=retport;
  89.    pkt->sp_Pkt.dp_Link=&pkt->sp_Msg;
  90.  
  91.    /* Set up packet args. First the packet request type. */
  92.    pkt->sp_Pkt.dp_Type=type;
  93.    
  94.    /* Handle each of the arguments in turn. */
  95.    /* Allow for variable number of parameters in a packet. */
  96.    va_start(vl,numarg);   
  97.    p=&pkt->sp_Pkt.dp_Arg1;
  98.    while(numarg--){
  99.       *p++=va_arg(vl,LONG);
  100.    }
  101.    va_end(vl);
  102.    
  103.    /* And finally, set the packet off on its way to the handler. */
  104.    PutMsg(procid,&pkt->sp_Msg);
  105.    
  106.    /*
  107.     * Wait for the reply. NOTE: Make SURE no other messages arrive first
  108.     * at this port!
  109.     */
  110.    while(!(msg=GetMsg(retport))) WaitPort(retport);
  111.    
  112.    if(res2)  /* Allow for NULL 'don't care' pointer. */
  113.       *res2=pkt->sp_Pkt.dp_Res2;
  114.    return pkt->sp_Pkt.dp_Res1;
  115. }
  116.  
  117.